package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.DirectorysMapper;
import com.itheima.mapper.LoginMapper;
import com.itheima.mapper.HmSubjectsDirectorysMapper;
import com.itheima.mapper.SubjectMapper;
import com.itheima.pojo.HmSubjects;
import com.itheima.pojo.HmSubjectsDirectorys;
import com.itheima.service.DirectorysService;
import com.itheima.util.HmSubjectsDirectorysDto;
import com.itheima.util.PageResult;
import com.itheima.util.SubjectsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class DirectorysServiceImpl implements DirectorysService {
    @Autowired
    private DirectorysMapper directorysMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private HmSubjectsDirectorysMapper hmSubjectsDirectorysMapper;

    @Override
    public PageResult findByPage(Integer page, Integer pagesize, String directoryName, Integer state) {
        Page<HmSubjectsDirectorys> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmSubjectsDirectorys> wrapper = new LambdaQueryWrapper();
        if(directoryName!=null){
            wrapper.like(HmSubjectsDirectorys::getDirectoryName,directoryName);
        }
        if(state!=null){
            wrapper.eq(HmSubjectsDirectorys::getState,state);
        }
        Page<HmSubjectsDirectorys> selectPage = directorysMapper.selectPage(p, wrapper);
        List<HmSubjectsDirectorys> records = selectPage.getRecords();
        //JSON.parseObject，是将Json字符串转化为相应的对象；
        //JSON.toJSONString则是将对象转化为Json字符串。
        String s = JSON.toJSONString(records);
        //将JSON字符串转换成List<HmSubjectsDirectorysDto>
        List<HmSubjectsDirectorysDto> list = JSON.parseArray(s,HmSubjectsDirectorysDto.class);
        for (HmSubjectsDirectorysDto hsdd:list) {
            Integer subjectID = hsdd.getSubjectID();
            Integer creatorID = hsdd.getCreatorID();
            hsdd.setUsername(directorysMapper.selectName(creatorID));
            hsdd.setSubjectName(directorysMapper.selectSubject(subjectID));
        }
        long pages1 = selectPage.getPages();
        return new PageResult(Integer.parseInt(selectPage.getTotal()+""),list,null,page+"",Integer.parseInt(pages1+""),pagesize+"");
    }

    @Override
    public void update(Integer id, Integer state) {
        HmSubjectsDirectorys hmSubjectsDirectorys = directorysMapper.selectById(id);
        hmSubjectsDirectorys.setState(state);
        directorysMapper.updateById(hmSubjectsDirectorys);

    }

    @Override
    public void updateAll(Integer id, HmSubjectsDirectorys hsd) {
        HmSubjectsDirectorys hmSubjectsDirectorys = directorysMapper.selectById(id);
        hmSubjectsDirectorys.setDirectoryName(hsd.getDirectoryName());
        hmSubjectsDirectorys.setSubjectID(hsd.getSubjectID());
        directorysMapper.updateById(hmSubjectsDirectorys);
    }

    @Override
    public void add(HmSubjectsDirectorys hsd) {
        String Authorization = request.getHeader("Authorization");
        String[] split = Authorization.split(" ");
        hsd.setCreatorID(loginMapper.selectprofig(split[1]));
        hsd.setAddDate(new Date());
        directorysMapper.insert(hsd);
    }

    @Override
    public void delete(Integer id) {
        directorysMapper.deleteById(id);
    }

    @Override
    public List<SubjectsResult> findById(Integer subjectID) {
        LambdaQueryWrapper<HmSubjectsDirectorys> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(HmSubjectsDirectorys::getSubjectID,subjectID);
        List<HmSubjectsDirectorys> hmSubjectsDirectorys = hmSubjectsDirectorysMapper.selectList(wrapper);
        //创建返回值对象
        List<SubjectsResult> subjectsResults = new ArrayList<>();
        for (HmSubjectsDirectorys hmSubjectsDirectory : hmSubjectsDirectorys) {
            SubjectsResult subjectsResult = new SubjectsResult();
            subjectsResult.setValue(hmSubjectsDirectory.getId());
            subjectsResult.setLabel(hmSubjectsDirectory.getDirectoryName());
            subjectsResults.add(subjectsResult);
        }
        return subjectsResults;
    }

}
